package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.geom.Matrix;
	
	import mx.controls.Image;
	
	import spark.components.Button;
	import spark.components.Label;
	import spark.components.SkinnableContainer;
	import spark.layouts.VerticalAlign;
	import spark.layouts.VerticalLayout;
	
	public class RobertFishEyeContainer extends SkinnableContainer
	{
		public function RobertFishEyeContainer()
		{
			super();
			
			layout = new RobertFishEyeLayout();
			updateElements(File.desktopDirectory);
			
			addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			
		}
		
		protected function updateElements(directory:File):void
		{
			var listing:Array = directory.getDirectoryListing();
			
			removeAllElements();

			if(directory.parent != null) {
				addElement(createImageContainer(directory.parent, true));
			}
			
			while(listing.length > 0) {
				var file:File = (listing.shift() as File);
				if(file.isDirectory || file.extension == 'jpg' || file.extension == 'gif' || file.extension == 'png') {
					var image:Image = new Image;
					if(file.isDirectory) {
						addElement(createImageContainer(file, false));
					} else {
						image.load(file.url);
						addElement(image);
					}
				}
			}
		}
		
		protected function onMouseMove(event:MouseEvent): void
		{
			(layout as RobertFishEyeLayout).onMouseMove(event);
		}
		
		protected function onRollOut(event:MouseEvent): void
		{
			(layout as RobertFishEyeLayout).onRollOut(event);
		}
		
		protected function addEventListenerInDirectory(container:SkinnableContainer, file:File):void 
		{
			container.addEventListener(MouseEvent.CLICK,  function():void {
				updateElements(file);
			});
		}
		/*
		protected function getFileIcon(file:File):Bitmap
		{
			var retainWidth:Number = 0;
			var retainBitmapData:BitmapData;
			var array:Array = file.icon.bitmaps;
			while(array.length > 0) {
				var bitmapData:BitmapData = (array.pop() as BitmapData);
				if(bitmapData.width > retainWidth) {
					retainWidth = bitmapData.width;
					retainBitmapData = bitmapData;
				}
			}
			return new Bitmap(retainBitmapData);
		}
		*/
		protected function createImageContainer(file:File, parent:Boolean):SkinnableContainer
		{
			var container:SkinnableContainer = new SkinnableContainer;
			[Embed(source='images/Folder.png')]
			var imageSource:Class
			var image:Image = new Image;
			var label:Label = new Label;
			var layoutContainer:VerticalLayout = new VerticalLayout;
			
			layoutContainer.horizontalAlign = 'center';
			image.source = imageSource;
			image.percentWidth = 100;
			image.percentHeight = 100;
			if(parent) {
				label.text = '../';
			}
			label.text += file.name;
			label.setStyle('color', '#FFFFFF');
			container.layout = layoutContainer;
			container.addElement(image);
			container.addElement(label);
			addEventListenerInDirectory(container, file);
//			
//			var m:Matrix = label.transform.matrix;
//			m.scale(2, 2);
//			label.transform.matrix = m;
			
			return container;
		}
	}
}